import config from '@/config/config.js';
import http from "@/api/http.js";


const defectRequest ={
    saveDefect(defect,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.defectBaseUrl + "/defect/save";
            http.post(url,defect,callBack);
        }else {
            callBack(undefined);
        }
    },
    updateDefectById(defect,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.defectBaseUrl + "/defect/update";
            http.put(url,defect,callBack);
        } else {
            callBack(undefined);
        }
    },
    selectById(defectId,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.defectBaseUrl + "/defect/get?defectId=" + defectId;
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },
    loadDefects(defect,page=1,limit=10,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = config.defectBaseUrl + "/defect/list?page="+page + "&limit="+limit;
            if(defect){
                if(defect.defectDescription){
                    url += "&defectDescription=" + defect.defectDescription;
                }
                if (defect.defectType){
                    url += "&defectType=" + defect.defectType;
                }
                if (defect.defectGrade){
                    url += "&defectGrade=" + defect.defectGrade;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },
    deletesProject(defectIds,callback){
        let token = localStorage.getItem("token");
        if(token){
            let url = config.defectBaseUrl + "/defect/deletes" + defectIds;
            if(defectIds){
                defectIds.forEach(id=>url += '&defectId=' + id)
            }
            http.delete(url,callback);
        }else {
            callback(undefined);
        }
    },
    deleteProject(defectId,callback){
        let token = localStorage.getItem("token");
        if(token){
            let url = config.defectBaseUrl + "/defect/delete?defectId="+defectId;
            http.delete(url,callback)
        }else {
            callback(undefined);
        }
    },






}

export default defectRequest